﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using Table_Dal;
using System.Data;
namespace BLL
{
    public class CauHoiBll
    {
        
        public DataTable LayDuLieuExcel(string DuongDan)
        {
            CauHoiDal ch = new CauHoiDal();
           return ch.LayDuLieuExcel(DuongDan);
        }
        public void Import(string DuongDan)
        {
            CauHoiDal ch = new CauHoiDal();
            ch.Import(DuongDan);
        }
        public bool XoaCauHoi(string MaCauHoi)
        {
            CauHoiDal ch = new CauHoiDal();
            return ch.XoaCauHoi(MaCauHoi);
        }
        public CauHoiTbl ThongTinCauHoi_TheoMaCauHoi_TenCauHoi(string MaCauHoi, string TenCauHoi)
        {
            CauHoiDal ch = new CauHoiDal();
            return ch.ThongTinCauHoi_TheoMaCauHoi_TenCauHoi(MaCauHoi, TenCauHoi);
        }
        public bool SuaCauHoi(CauHoiTbl cauhoi)
        {
            CauHoiDal ch = new CauHoiDal();
            return ch.SuaCauHoi(cauhoi);
        }
        public bool ThemCauHoi(CauHoiTbl cauhoi)
        {
            CauHoiDal ch = new CauHoiDal();
            return ch.ThemCauHoi(cauhoi);
        }
        public bool KiemTra_TrungKhoa(string Ma)
        {
            CauHoiDal ch = new CauHoiDal();
            return ch.KiemTra_TrungKhoa(Ma);
        }
        public List<CauHoiTbl> LayDanhSachCauHoi_MaChuong_DoKho_MaMon(string MaChuong, string Dokho, string mamon)
        {
            CauHoiDal ch = new CauHoiDal();
            ChuongDal chg = new ChuongDal();
            MonHocBLL mh = new MonHocBLL();
            Dokho = DoKho(Dokho);
            return ch.LayDanhSachCauHoi_MaChuong_DoKho_MaMon1(MaChuong, int.Parse(Dokho), mamon);
        }
        public string DoKho(string s)
        {
            if (s == "Tất cả")
                s = 0.ToString();
            if (s == "Rất dễ")
                s = 1.ToString();
            if (s == "Dễ")
                s = 2.ToString();
            if (s == "Vừa")
                s = 3.ToString();
            if (s == "Khó")
                s = 4.ToString();
            if (s == "Rất khó")
                s = 5.ToString();
            return s;
        }

        public List<CauHoiTbl> DanhsachCauHoiDaChon(List<string> dscauhoi, List<string> dscauhoidachon, int socau, string MaChuong, string dokho, string MaMon)
        {
            List<CauHoiTbl> DSCauHoi = new List<CauHoiTbl>();
            
            if (dscauhoi.Count > 0)
            {
                for (int i = 0; i < dscauhoi.Count; i++)
                {
                    CauHoiTbl ch = new CauHoiTbl();
                    ch = ThongTinCauHoi_TheoMaCauHoi_TenCauHoi(dscauhoi[i],"");
                    DSCauHoi.Add(ch);
                }
            }
            if (socau > 0)
            {
                List<string> cauHoiPhu = new List<string>();
                MonHocDAL mh = new MonHocDAL();
                CauHoiDal ch = new CauHoiDal();
                ChuongDal chg = new ChuongDal();
                cauHoiPhu = ch.LayDanhSachCauHoi_MaChuong_DoKho_MaMon(MaChuong, int.Parse(DoKho(dokho)), MaMon);
                List<string> AllCauhoi = new List<string>();
                foreach (string temp1 in dscauhoi)
                {
                    AllCauhoi.Add(temp1);
                }
                foreach (string temp in dscauhoidachon)
                {
                    AllCauhoi.Add(temp);
                }
                for (int i = 0; i < cauHoiPhu.Count; i++)
                {
                    int k = 0;
                    foreach (string temp in AllCauhoi)
                    {
                        if (cauHoiPhu[i] == temp)
                        {
                            k = 1;
                        }
                    }
                    if (k == 1)
                    {
                        cauHoiPhu.Remove(cauHoiPhu[i]);
                        i--;
                    }
                }

                int x = cauHoiPhu.Count;
                if (x > 0)
                {
                    if (x < socau)
                    {
                        foreach (string i in cauHoiPhu)
                        {
                            DSCauHoi.Add(ch.ThongTinCauHoi_TheoMaCauHoi_TenCauHoi(i, ""));
                        }
                    }
                    else
                    {
                        BusinessLogicLayer bll = new BusinessLogicLayer();
                        int i = 0;
                        while (i < socau)
                        {
                            int IntRandom = bll.RandomIndex(cauHoiPhu.Count);
                            int k = 0;

                            foreach (CauHoiTbl cauhoi in DSCauHoi)
                            {
                                if (cauHoiPhu[IntRandom] == cauhoi.MaCauHoi)
                                    k = 1;
                            }
                            if (k == 0)
                            {
                                DSCauHoi.Add(ch.ThongTinCauHoi_TheoMaCauHoi_TenCauHoi(cauHoiPhu[IntRandom], ""));
                                i++;
                            }
                        }
                    }
                }
            }
            return DSCauHoi;
        }
        public List<CauHoiTbl> TimKiemCauhoi(string chuoi, List<string> dscauhoidachon, string MaMon,string Dokho,string Chuong)
        {
            CauHoiDal ch = new CauHoiDal();
            List<CauHoiTbl> dscauhoi_Find = new List<CauHoiTbl>();
            string dk = DoKho(Dokho);
            dscauhoi_Find = ch.Timkiemcauhoi(chuoi, MaMon, int.Parse(dk), Chuong);
            for (int i = 0; i < dscauhoi_Find.Count;i++ )
            {
                int k = 0;
                foreach (string j in dscauhoidachon)
                {
                    if (dscauhoi_Find[i].MaCauHoi == j)
                        k = 1;
                }
                if (k == 1)
                {
                    dscauhoi_Find.Remove(dscauhoi_Find[i]);
                    i--;
                }
            }
            return dscauhoi_Find;
        }

        
        public string PictureLink(string Macauhoi)
        {
            CauHoiDal chdal = new CauHoiDal();
            return chdal.PictureLink(Macauhoi);
        }
        public List<CauHoiTbl> DSCauHoiDeThi(string Ma)
        {
            CauHoiDal chdal = new CauHoiDal();
            return chdal.DSCauHoiDeThi(Ma);
        }

        public List<string> LayTatCa_string()
        {
            CauHoiDal chdal = new CauHoiDal();
            return chdal.LayTatCa_string();
        }
    }
}
